package com.qinghe.plant.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qinghe.plant.util.CodeType;
import com.qinghe.plant.util.Response;
import com.qinghe.plant.dao.ModelType;
import com.qinghe.plant.model.ModelTypeResponsitory;

@RestController
public class ModelTypeController {

	@Autowired
	private ModelTypeResponsitory modelTypeResponsitory;

	@RequestMapping(value = "doReadModelTypes", method = RequestMethod.GET)
	public Response ReadModelTypes(HttpServletRequest request) {
		List<ModelType> modelTypes= modelTypeResponsitory.findAll();
		return new Response(CodeType.Success,JSONArray.toJSONString(modelTypes),"操作成功");
	}
	
	@RequestMapping(value = "doReadModelType", method = RequestMethod.POST)
	public Response ReadModelType(HttpServletRequest request) {
		String id =request.getParameter("id");
		ModelType modelType = FindModelType(id);
		return new Response(CodeType.Success,JSONObject.toJSONString(modelType),"操作成功");
	}

	@RequestMapping(value = "doDeleteModelType", method = RequestMethod.POST)
	public Response DeleteModelType(HttpServletRequest request) {
		String id =request.getParameter("id");
		modelTypeResponsitory.deleteById(id);
		return new Response(CodeType.Success,null,"操作成功");
	}
	
	@RequestMapping(value="doAlterModelType",method=RequestMethod.POST)
	public Response AlterModelType(HttpServletRequest request)
	{
		ModelType modelType=JSONObject.parseObject(request.getParameter("modelType"), ModelType.class);
		modelType= modelTypeResponsitory.save(modelType);
		return new Response(CodeType.Success,JSONArray.toJSONString(modelType),"操作成功");		
	}
	
	@RequestMapping(value="doInsertModelType",method=RequestMethod.POST)
	public Response InsertModelType(HttpServletRequest request)
	{
		if(HasModelType(request.getParameter("name")))
		{
			return new Response(CodeType.Failed,null,"类型已经存在！");
		}
		else 
		{
			ModelType modelType=new ModelType();
			modelType.setName(request.getParameter("name"));
			modelType.setCreator(request.getParameter("creator"));			
			modelType.setTimer(new Date());
			modelType= modelTypeResponsitory.save(modelType);
			return new Response(CodeType.Success,JSONObject.toJSONString(modelType),"操作成功");
		}
	}
	
	private ModelType FindModelType(String id) {
		@SuppressWarnings("serial")
		List<ModelType> result = modelTypeResponsitory.findAll(new Specification<ModelType>() {
			@Override
			public Predicate toPredicate(Root<ModelType> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				list.add(cb.equal(root.get("id").as(String.class), id));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		});

		return (result.size()!=0)?result.get(0):null;
	}	
	
	private boolean HasModelType(String name) {
		@SuppressWarnings("serial")
		List<ModelType> result = modelTypeResponsitory.findAll(new Specification<ModelType>() {
			@Override
			public Predicate toPredicate(Root<ModelType> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				list.add(cb.equal(root.get("name").as(String.class), name));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		});

		return (result.size()!=0)?true:false;
	}	
}
